package com.bocloud.cmp.driver.providers.vmware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.ClusterDriver;
import com.bocloud.cmp.model.ClusterModel;
import com.bocloud.cmp.model.ClusterOperateModel;
import com.bocloud.common.model.BsmResult;
import com.vmware.vim25.ClusterConfigSpecEx;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datacenter;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.Task;

/**
 * vmware集群操作接口实现类
 * 
 * @author weiwei
 *
 */
public class VmwareClusterDriver extends VmwareDriver implements ClusterDriver {

	private static Logger logger = LoggerFactory.getLogger(VmwareClusterDriver.class);

	public VmwareClusterDriver() {
		super();
	}

	/**
	 * @param endpoint
	 * @param username
	 * @param password
	 */
	public VmwareClusterDriver(String endpoint, String username, String password) {
		super(endpoint, username, password);
	}

	@Override
	public BsmResult create(ClusterOperateModel model) {
		try {
			ManagedEntity managedEntity = new InventoryNavigator(this.getRootFolder()).searchManagedEntity(DC,
					model.getVdcName());
			if (null == managedEntity) {
				return new BsmResult(false, "创建集群失败");
			}
			Datacenter datacenter = (Datacenter) managedEntity;
			// 获取该数据中心下的所有的集群
			ManagedEntity[] childEntities = datacenter.getHostFolder().getChildEntity();
			for (ManagedEntity entity : childEntities) {
				if (entity instanceof ClusterComputeResource) {
					if (model.getClusterName().equals(entity.getName())) {
						return new BsmResult(false, "数据中心已存在该名称");
					}
				}
			}
			// 创建集群
			ClusterConfigSpecEx clusterConfigSpec = new ClusterConfigSpecEx();
			ClusterComputeResource clusterResult = datacenter.getHostFolder().createClusterEx(model.getClusterName(),
					clusterConfigSpec);
			if (null == clusterResult) {
				return new BsmResult(false, "创建集群失败");
			}
			return new BsmResult(true, assembleCluster(clusterResult), "创建集群成功");
		} catch (Exception e) {
			logger.error("create cluster error:", e);
			return new BsmResult(false, "创建集群失败");
		} finally {
			this.getServiceInstance().getServerConnection().logout();
		}
	}

	@Override
	public BsmResult remove(ClusterOperateModel model) {
		try {
			// 获取数据中心
			Datacenter datacenter = (Datacenter) new InventoryNavigator(this.getRootFolder()).searchManagedEntity(DC,
					model.getVdcName());
			if (null == datacenter) {
				return new BsmResult(false, "删除集群失败：未获取到集群所在数据中心");
			}
			// 获取要删除的集群
			ManagedEntity cluster = new InventoryNavigator(datacenter).searchManagedEntity("ClusterComputeResource",
					model.getClusterName());
			if (null != cluster) {
				if (cluster instanceof ClusterComputeResource) {
					// 删除集群任务
					Task task = cluster.destroy_Task();
					if (task.waitForTask().equals(Task.SUCCESS)) {
						return new BsmResult(true, "删除集群成功");
					}
				}
			}else{
				return new BsmResult(false, "不存在该集群，请同步平台数据");
			}
			return new BsmResult(false, "删除集群失败");
		} catch (Exception e) {
			logger.error("删除集群失败", e);
			return new BsmResult(false, "删除集群失败");
		} finally {
			this.getServiceInstance().getServerConnection().logout();
		}
	}

	/**
	 * 组装主机集群相关元数据
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	private ClusterModel assembleCluster(ClusterComputeResource entity) throws Exception {
		ClusterModel cluster = new ClusterModel();
		cluster.setName(entity.getName());
		cluster.setClusterVal(entity.getMOR().getVal());
		cluster.setTemplateNum(0);
		cluster.setVmNum(0);
		cluster.setHostNum(0);
		cluster.setCpuTotal(0L);
		cluster.setMemTotal(0L);
		cluster.setDiskTotal(0L);
		cluster.setCpuUsed(0L);
		cluster.setMemUsed(0L);
		cluster.setDiskUsed(0L);
		cluster.setCpuCore(0);
		return cluster;
	}

}
